#ifndef MEGAMAN_H
#define MEGAMAN_H

#include "UtilFunctions.h"
#include "GlobalSettings.h"
#include "MathMisc.h"

class MegaMan
{
	public:
		friend class Camera;
		MegaMan();
		MegaMan(float x, float y, float z);
		MegaMan(Position point);
		void setMega(Position point);

		void Update();

		void HandleInput();

		MegaMan & operator = (MegaMan * Mega)
		{
			location = Mega->location;
			velocity = Mega->velocity;
			return *this;
		}

	private:
		Position location;
		Position velocity;
		float cameraMod;
		float Matrix[4][4];
		bool isMoving;

		void Move();

		void IdleAnimation();
};

MegaMan::MegaMan()
{ 
	location = 0; 
	isMoving = false; 
	cameraMod = 0;
}

MegaMan::MegaMan(float x, float y, float z)
{ 
	location = Position(x, y, z); 
	isMoving = false; 
	cameraMod = 0;
}

MegaMan::MegaMan(Position point)
{ 
	location = point; 
	isMoving = false;
	cameraMod = 0; 
}

void MegaMan::setMega(Position point)
{
	location = point; 
	isMoving = false;
	cameraMod = 0; 
}

void MegaMan::HandleInput()
{
	float angle = 0;
	isMoving = true;
	if (KeyDownSpecial[0])
		angle = 90;
	else if (KeyDownSpecial[1])
		angle = 270;
	else
		angle = 0;

	if (KeyDownSpecial[2] && !angle)
		angle = 180;
	else if (KeyDownSpecial[3] && !angle)
		angle = 0;
	else if (KeyDownSpecial[2] && angle)
		angle += angle == 90 ? 45 : -45;
	else if (KeyDownSpecial[3] && angle)
		angle += angle == 270 ? 45 : -45;
	else if (!angle)
		isMoving = false;

	velocity.angle = angle + cameraMod;
}

void MegaMan::Update()
{
	// Play next animation frame from here.
	Move();

	IdleAnimation();
}

void MegaMan::Move()
{
	if (isMoving)
	{
		location.x += MSPEED*sin(toRadian(velocity.angle));
		location.z += MSPEED*cos(toRadian(velocity.angle));
	}
}

void MegaMan::IdleAnimation()
{
	// Lets draw MegaManCube
	glPushMatrix();

	glTranslatef(location.x, location.y, location.z);
	glRotatef(velocity.angle, 0, 1, 0);
	glScalef(1, 2.5f, 1);
	glColor3f(0.0f, 0.0f, 1.0f);
	glutSolidCube(32.0f);

	glPopMatrix();
}

#endif
